﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.EnterpriseServices;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Text;

namespace Inventory.Test.Library.Plugin
{
    [Description("Stores four dates in the test context [ContextParameterName], [ContextParameterName]Month, [ContextParameterName]Year, [ContextParameterName]Day")]
    public class GenerateDatesPlugin : WebTestPlugin
    {
        string _ContextParameterName = "Now";
        public string ContextParameterName
        {
            get
            {
                if (string.IsNullOrEmpty(_ContextParameterName))
                    _ContextParameterName = "Now";

                return _ContextParameterName;
            }
            set
            {
                _ContextParameterName = value;
            }
        }
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            DateTime temp = DateTime.Now;
            e.WebTest.Context.Add(this.ContextParameterName, temp.Ticks.ToString());
            e.WebTest.Context.Add(this.ContextParameterName + "Month", temp.Month.ToString());
            e.WebTest.Context.Add(this.ContextParameterName + "Year", temp.Year.ToString());
            e.WebTest.Context.Add(this.ContextParameterName + "Day", temp.Day.ToString());

            char[] chars = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };


            var firstNumber = new Random(1).Next(0, 9);
            var secondNumber = new Random(1).Next(0, 9);


            string randomString = RandomString(2);
            randomString += RandomNumbersString(2);

            e.WebTest.Context.Add("4RandomChars", randomString);

            var randomNumberString = RandomNumbersString(2);
            e.WebTest.Context.Add("2RandomDigits", randomNumberString);

        }
        private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden

        /// <summary>
        /// Generates a random string with the given length
        /// </summary>
        /// <param name="size">Size of the string</param>
        /// <param name="lowerCase">If true, generate lowercase string</param>
        /// <returns>Random string</returns>
        private string RandomString(int size)
        {
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }

            return builder.ToString();
        }
        /// <summary>
        /// Generates a random string with the given length
        /// </summary>
        /// <param name="size">Size of the string</param>
        /// <param name="lowerCase">If true, generate lowercase string</param>
        /// <returns>Random string</returns>
        private string RandomNumbersString(int size)
        {
            StringBuilder builder = new StringBuilder();
            int ch;
            for (int i = 0; i < size; i++)
            {
                ch = random.Next(9);
                builder.Append(ch);
            }

            return builder.ToString().Substring(0, size);
        }
    }
}
